package com.atguigu.srb.core.controller.api;


import com.atguigu.common.results.R;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.pojo.entity.LendItemReturn;
import com.atguigu.srb.core.service.LendItemReturnService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 标的出借回款记录表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/api/core/lendItemReturn")
public class ApiLendItemReturnController {
    @Autowired
    LendItemReturnService lendItemReturnService;
    @ApiOperation("查询当前用户的回款计划")
    @GetMapping("/list/{lendId}")
    public R list(@RequestHeader(value = "token" , required = false)String token,
                  @PathVariable("lendId")String lendId){
        try {
            Long userId = JwtUtils.getUserId(token);
            List<LendItemReturn> lendItemReturns = lendItemReturnService.list(new LambdaQueryWrapper<LendItemReturn>()
                    .eq(LendItemReturn::getInvestUserId, userId)
                    .eq(LendItemReturn::getLendId, lendId)
                    .orderByAsc(LendItemReturn::getCurrentPeriod));
            return R.success().data("items",lendItemReturns);
        } catch (Exception e) {
            //用户未登录  JwtUtils.getUserId(token)解析token会抛出异常，此处捕获手动处理 返回空数据 表示没有查询到回款计划
            return R.success();
        }

    }


}

